<?php

// +----------------------------------------------------------------------
// | MC PHP[基于ThinkPHP6.1开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://www.mingchangruanjian.com
// +----------------------------------------------------------------------
// | Author jokerfei [ jokerfei377@gmail.com ]
// +----------------------------------------------------------------------
// | 铭畅软件 技术部 出品
// +----------------------------------------------------------------------


namespace app\api\controller\v1;

use app\api\controller\Base;
use app\common\service\ApiReturn;
use think\facade\Db;
use app\common\model\Order;
use app\admin\model\user\User;
/**
 * 运营管理
 */
class Operation extends Base
{
    /** 
     * @Author: Bryant  
     * @Date: 2023-06-14 09:47:33 
     * @method post 
     * @title 轮播图列表 
     */
    public function navList($data=[],$user=[])
    {
        $where[] = ['status','=',1];
        $where[] = ['type','=',$data['type']];
        $list = \app\admin\model\operation\OperationNav::where($where)->order('sort desc')->field('id,name,thumb,href')->select();
        return ApiReturn::r(1,$list,'请求成功');
    }
    /** 
     * @Author: Bryant  
     * @Date: 2023-06-14 14:25:41 
     * @method post 
     * @title 平台公告 
     */
    public function noticeList($data=[],$user=[])
    {
        $user_id = $user['id'];
        $list = \app\admin\model\operation\OperationNotice::where('status',1)
        ->field('id,desc,title,thumb,create_time')
        ->order('create_time desc')
        ->paginate()
        ->each(function($item) use ($user_id){
            $item['is_read'] = Db::name('operation_notice_read')->where('notice_id',$item['id'])->where('user_id',$user_id)->find() ? 1 : 0;
            return $item;
        });
        return ApiReturn::r(1,$list,'请求成功');
    }
    /** 
     * @Author: Bryant  
     * @Date: 2023-06-14 14:34:52 
     * @method post 
     * @title 将公告标记为已读 
     */
    public function noticeRead($data=[],$user=[])
    {
        $info = Db::name('operation_notice_read')
        ->where('notice_id',$data['notice_id'])
        ->where('user_id',$user['id'])
        ->find();
        if($info){
            return ApiReturn::r(1,[],'请求成功');
        }
        Db::name('operation_notice_read')->insert([
            'notice_id' => $data['notice_id'],
            'user_id' => $user['id'],
            'create_time' => time()
        ]);
        return ApiReturn::r(1,[],'请求成功');
    }
    /** 
     * @Author: Bryant  
     * @Date: 2023-06-14 14:58:26 
     * @method post 
     * @title 一键反馈类型 
     */
    public function suggestionsType($data=[],$user=[])
    {
        $list = \app\admin\model\operation\OperationSuggestionsType::where('status',1)->field('id,name')->select();
        return ApiReturn::r(1,$list,'请求成功');
    }
    /** 
     * @Author: Bryant  
     * @Date: 2023-06-14 15:28:17 
     * @method post 
     * @title 添加意见反馈 
     */
    public function addSuggestions($data=[],$user=[])
    {
        $info = \app\admin\model\operation\OperationSuggestions::where('user_id',$user['id'])->find();
        if($info && $info['create_time'] < time() - 86400){
            return ApiReturn::r(0,[],'每天只能反馈一次');
        }
        $data['user_id'] = $user['id'];
        $data['create_time'] = time();
        $res = \app\admin\model\operation\OperationSuggestions::insert($data);
        if(!$res){
            return ApiReturn::r(0,[],'反馈失败');
        }
        return ApiReturn::r(1,[],'反馈成功');
    }
    /** 
     * @Author: Bryant  
     * @Date: 2023-06-14 15:42:21 
     * @method post 
     * @title 我的意见反馈 
     */
    public function suggestionsList($data=[],$user=[])
    {
        $list = \app\admin\model\operation\OperationSuggestions::where('user_id',$user['id'])
        ->paginate()
        ->each(function($item){
            
            return $item;
        });
        return ApiReturn::r(1,$list,'请求成功');
    }
    /** 
     * @Author: Bryant  
     * @Date: 2023-06-29 17:32:40 
     * @method post 
     * @title 帮助中心 
     */
    public function help($data=[],$user=[])
    {
        $list = Db::name('operation_help')->where('status',1)
        ->field('id,title')
        ->paginate();
        return ApiReturn::r(1,$list,'请求成功');
    }
    /** 
     * @Author: Bryant  
     * @Date: 2023-06-30 14:45:27 
     * @method post 
     * @title 帮助中心详情 
     */
    public function helpDetail($data=[],$user=[])
    {
        $content = Db::name('operation_help')->where('id',$data['id'])
        ->value('content');
        return ApiReturn::r(1,['content' => $content],'请求成功');
    }
}